{% extends 'layout/basic.html' %}
{% load static %}
{% block title %}
    用户注册
{% endblock %}
{% block css %}
    <style>
        .account {
            width: 400px;
            margin: 0 auto;
        }

        h1 {
            text-align: center;
        }

        .error-msg {
            color: red;
            position: absolute;
            font-size: 13px;
        }

    </style>
{% endblock %}


{% block content %}
    <div class="account">
        <h1>注册</h1>
        <form id="regForm" method="post" novalidate>
            {% csrf_token %}
            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        <div class="clearfix">
                            <div class="col-md-6" style="padding-left: 0;">
                                {{ field }}
                                <span class="error-msg"></span>
                            </div>
                            <div class="col-md-6"><input type="button" id="btnSms" class="btn btn-default"
                                                         value="点击获取验证码">
                            </div>
                        </div>
                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {{ field }}
                        <span class="error-msg"></span>
                    </div>
                {% endif %}
            {% endfor %}
        </form>
        <div class="row">
            <div class="col-xs-3">
                <input id="btnSubmit" type="button" class="btn btn-primary" value="注  册"/>
            </div>
        </div>
    </div>


{% endblock %}

{% block js %}
    <script>
        $(function () {
            bindClickBtnSms();
            bindClickSubmit();
        })

        /*
            点击验证码发送短信
         */
        function bindClickBtnSms() {
            $('#btnSms').click(function () {
                $('.error-msg').empty()
                // 获取用户的手机号
                var mobile_phone = $('#id_mobile_phone').val();

                $.ajax({
                    url: "{% url 'send_sms'%}",
                    type: "GET",
                    data: {mobile_phone: mobile_phone, tpl: 'register'},
                    dataType: 'JSON',
                    success: function (res) {
                        //console.log(res)
                        if (res.status) {
                            // 启动倒计时功能
                            sendSmsRemind();
                        } else {
                            // 错误 显示对应的错误信息
                            $.each(res.error, function (key, value) {
                                $('#id_' + key).next().text(value[0])
                            });
                        }
                    }
                })
            })
        }

        /*
            倒计时功能
         */
        function sendSmsRemind() {
            var $smsBtn = $('#btnSms');
            // 禁用状态
            $smsBtn.prop('disabled', true)
            var time = 60;
            var remind = setInterval(function () {
                $smsBtn.val(time + '秒重新发送');
                time -= 1;
                if (time < 1) {
                    clearInterval(remind);
                    $smsBtn.val('点击获取验证码').prop('disabled', false);
                }
            }, 1000)
        }

        /*
            实现注册
         */
        function bindClickSubmit() {
            $('#btnSubmit').click(function () {
                $('.error-msg').empty();
                $.ajax({
                    url: '{% url 'register' %}',
                    type: 'POST',
                    data: $('#regForm').serialize(),
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.status) {
                            // 跳转到登录页面
                            location.href = res.data
                        } else {
                            // 错误 显示对应的错误信息
                            $.each(res.error, function (key, value) {
                                $('#id_' + key).next().text(value[0])
                            });
                        }
                    }
                });
            });
        }

    </script>
{% endblock %}


